//
//  TZShowCell.m
//  BSBDJ
//
//  Created by user on 16/5/25.
//  Copyright © 2016年 Burning. All rights reserved.
//
#import "UIView+Frame.h"
#import "TZShowCell.h"
#import "TZEssenceItem.h"
#import "UIImage+image.h"



#import "TZShowImageView.h"
#import "TZShowVoiceView.h"
#import "TZShowVideoView.h"
@interface TZShowCell()

@property (weak, nonatomic) IBOutlet UIView *topComment_View;
@property (weak, nonatomic) IBOutlet UILabel *text_Label;
@property (weak, nonatomic) IBOutlet UIImageView *icon_ImageV;
@property (weak, nonatomic) IBOutlet UILabel *name_Label;
@property (weak, nonatomic) IBOutlet UILabel *time_Label;
@property (weak, nonatomic) IBOutlet UIButton *zan_Btn;
@property (weak, nonatomic) IBOutlet UIButton *cai_Btn;
@property (weak, nonatomic) IBOutlet UIButton *comment_Btn;
@property (weak, nonatomic) IBOutlet UIButton *share_Btn;
@property (weak, nonatomic) IBOutlet UILabel *fileComment_Label;

@property (weak, nonatomic) TZShowImageView *showImageView;
@property (weak, nonatomic) TZShowVoiceView *showVoiceView;
@property (weak, nonatomic) TZShowVideoView *showVideoView;
@end

@implementation TZShowCell
- (IBAction)clickShareBtn:(id)sender {
    NSLog(@"分享");
    if (_block) {
        _block(self.item);
    }
}
///图片类型的View
- (TZShowImageView *)showImageView{
    
    if (_showImageView == nil) {
        TZShowImageView *showImageView = [TZShowImageView showImageView];
        [self.contentView addSubview:showImageView];
        _showImageView = showImageView;
    }
    return _showImageView;
}
///声音乐行的VieW
- (TZShowVoiceView *)showVoiceView{
    
    if (_showVoiceView == nil) {
        TZShowVoiceView *showVoiceView = [TZShowVoiceView showVoiceView];
        [self.contentView addSubview:showVoiceView];
        _showVoiceView = showVoiceView;
    }
    return _showVoiceView;
}
///视频内容View
- (TZShowVideoView *)showVideoView{
    if (_showVideoView == nil) {
        TZShowVideoView *showVideoView = [TZShowVideoView showVideoView];
        [self.contentView addSubview:showVideoView];
        _showVideoView = showVideoView;
    }
    return _showVideoView;
}

#pragma mark --------------
#pragma mark 功能按钮点击
- (IBAction)zanBtnClik {
    
    [self touchAnimateWithFuncBtn:self.zan_Btn];
}
- (IBAction)caiBtnClick {
    
    [self touchAnimateWithFuncBtn:self.cai_Btn];

}
- (IBAction)commentBtnClick {
    
    [self touchAnimateWithFuncBtn:self.comment_Btn];
}
- (IBAction)shareBtnClick {
    

    if (_block) {
        _block(self.item);
    }
}


- (void)touchAnimateWithFuncBtn :(UIButton *)btn{
    static NSTimeInterval animaTime = 0.2;
  
    if (btn.selected) {
        // 没有选中
        //在动画中设置imageView的image
        
        [UIView animateWithDuration:animaTime animations:^{
            btn.imageView.transform = CGAffineTransformMakeScale(1.7, 1.7);
        } completion:^(BOOL finished) {
              btn.selected = !btn.selected;
            [UIView animateWithDuration:animaTime animations:^{
                  btn.imageView.transform = CGAffineTransformMakeScale(0.9, 0.9);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:animaTime animations:^{
                    btn.imageView.transform = CGAffineTransformMakeScale(1, 1);
                } completion:^(BOOL finished) {
                    
                }];
            }];
        }];
    }else{
    // 选中
    
    [UIView animateWithDuration:animaTime animations:^{
      btn.imageView.transform = CGAffineTransformMakeScale(1.7, 1.7);
    } completion:^(BOOL finished) {
        btn.selected = !btn.selected;
        
        [UIView animateWithDuration:animaTime animations:^{
              btn.imageView.transform = CGAffineTransformMakeScale(0.9, 0.9);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:animaTime animations:^{
                  btn.imageView.transform = CGAffineTransformMakeScale(1, 1);
            } completion:^(BOOL finished) {
                
            }];
        }];
    }];
    }
}





#pragma mark --------------
#pragma mark 设置模型属性
- (void)setItem:(TZEssenceItem *)item{
    _item = item;
    [self.icon_ImageV sd_setImageWithURL:[NSURL URLWithString:item.profile_image] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.icon_ImageV.image = [image circleImage];
    }];
    
    self.name_Label.text = item.name;
    self.time_Label.text = item.created_at;
    self.text_Label.text = item.text;
    [self setCellButton:self.zan_Btn title:item.ding];
    [self setCellButton:self.cai_Btn title:item.cai];
    [self setCellButton:self.share_Btn title:item.repost];
    [self setCellButton:self.comment_Btn title:item.comment];
    
    //热门评论
    if (item.top_cmt.count) {
        self.topComment_View.hidden = NO;
        NSString *username = item.top_cmt[0][@"user"][@"username"];
        NSString *content = item.top_cmt[0][@"content"];
        if (content.length == 0) { // 没有文字内容，是个语音评论
            content = @"[语音评论]";
        }
        NSString *hotLabel = [NSString stringWithFormat:@"%@ : %@",username,content];
        self.fileComment_Label.text = hotLabel;
    }else{
        self.topComment_View.hidden = YES;
    }
    
    if (item.type == TZTopicTypePicture) {
        self.showImageView.hidden = NO;
        self.showVideoView.hidden = YES;
        self.showVoiceView.hidden = YES;
        self.showImageView.item = item;
        self.showImageView.frame = item.contentViewFrame;

    }else if (item.type == TZTopicTypeVoice){
        self.showVoiceView.hidden = NO;
        self.showVideoView.hidden = YES;
        self.showImageView.hidden = YES;
        self.showVoiceView.item = item;
        self.showVoiceView.frame = item.contentViewFrame;

    }else if(item.type == TZTopicTypeVideo){
        self.showVoiceView.hidden = YES;
        self.showImageView.hidden = YES;
        self.showVideoView.hidden = NO;
        self.showVideoView.item = item;
        self.showVideoView.frame = item.contentViewFrame;
    }else{
        self.showImageView.hidden = YES;
        self.showVoiceView.hidden = YES;
        self.showVideoView.hidden = YES;
    }
    
    NSLog(@"%@---------------",item.ID);
        
    
}

#pragma mark 给顶 分享 踩按钮设置内容
- (void)setCellButton :(UIButton*)button title:(NSInteger)title
{
    NSString *titleCount;
    if (title>10000) {
        NSInteger count = title/10000.0;
        titleCount = [NSString stringWithFormat:@"%ld万",(long)count];
    }else if (title>0){
        titleCount = [NSString stringWithFormat:@"%ld",(long)title];
    }
    [button setTitle:titleCount forState:0];

}

- (void)setFrame:(CGRect)frame{
    frame.origin.x += 5;
    frame.size.width -= 10;
    frame.size.height -= 10;
    frame.origin.y += 10;
    [super setFrame:frame];
}
@end
